package com.xneure.database;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import javax.sql.DataSource;
import com.xneure.database.dialect.MySqlDialect;
import com.xneure.database.dialect.OracleDialect;
import com.xneure.database.dialect.SqlDialect;
import com.xneure.database.dialect.SqlServerDialect;
import com.xneure.utils.ObjectUtil;

public class DataSourceContext{
	private String currDataSource;
	private Method currMethod;
	private DataSource lastDataSource;
	private boolean isLock = false;
	public static Map<String,SqlDialect> sqlDialect = new HashMap<>();
	static{
		DataSourceContext.sqlDialect.put(DataBaseType.Oracle.getDbName(),new OracleDialect());
		DataSourceContext.sqlDialect.put(DataBaseType.MySql.getDbName(),new MySqlDialect());
		DataSourceContext.sqlDialect.put(DataBaseType.SqlServer.getDbName(),new SqlServerDialect());
	}

	public String getCurrDataSource(){
		return currDataSource;
	}

	public DataSourceContext setCurrDataSource(String currDataSource){
		if(isLock == false){
			this.currDataSource = currDataSource;
		}
		return this;
	}

	public Method getCurrMethod(){
		return currMethod;
	}

	public DataSourceContext setCurrMethod(Method method){
		this.currMethod = method;
		return this;
	}

	public DataSource getLastDataSource(){
		return lastDataSource;
	}

	public String getDataSourceInfo(){
		String fieldValue = ObjectUtil.getFieldValue(lastDataSource,"driverClass");
		return fieldValue + "-->" + ObjectUtil.getFieldValue(lastDataSource,"jdbcUrl") + "@" + ObjectUtil.getFieldValue(lastDataSource,"username") + ":"
				+ ObjectUtil.getFieldValue(lastDataSource,"password");
	}

	public void setLastDataSource(DataSource lastDataSource){
		this.lastDataSource = lastDataSource;
	}

	public boolean isLock(){
		return isLock;
	}

	public void setLock(boolean isLock){
		this.isLock = isLock;
	}
}
